/*********************************************************************************
*  Copyright (C) 2010  by Zhang Shine 
*  E-Mail : zhangshine0125@gmail.com
*  
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*  
*  http://www.apache.org/licenses/LICENSE-2.0
*  
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 
*  express or implied. See the License for the specific language 
*  governing permissions and limitations under the License.
**********************************************************************************/

#include "dictionary.h"

#include <fstream>
#include <iostream>
#include <string>
#include <cstring>

bool Dictionary::readLine(fstream &inputFile, string &textLine)
{
    while(getline( inputFile, textLine, '\n'))
    {
	int prevPos = 0;
	int postPos = 0;
	while(textLine[prevPos] == ' ' || textLine[prevPos] == '\t' || textLine[prevPos] == '\r')
	{
	    prevPos++;
	}
	if(textLine[prevPos] == '\0')
	    continue;
	else
	{
	    postPos = textLine.length()-1;
	    while(textLine[postPos] == ' ' || textLine[postPos] == '\t' || textLine[postPos] == '\r')
		postPos--;
	    textLine = textLine.substr(prevPos, postPos+1-prevPos);
	    return true;//read a line successfully.
	}
    }
    return false; //EOF
}

bool Dictionary::skipBOM(fstream &inputFile)
{
    inputFile.seekg( 0, ios::beg );
    string firstLine;
    char src[3],des[3];
    des[0]='\xEF';des[1]='\xBB';des[2]='\xBF';
    readLine( inputFile, firstLine );
    if(firstLine.length()>=3)
    {
	src[0] = firstLine[0]; src[1] = firstLine[1]; src[2] = firstLine[2];
	if( memcmp(src, des, 24)==0 )
	{
	    inputFile.seekg( 3, ios::beg );
	    return true;
	}
    } 
    return false;
}

Dictionary::Dictionary()
{
    
}

bool Dictionary::loadFromFile( char *fileName )
{
    
}

bool Dictionary::save( char *fileName )
{
    
}

